//
//  YcKeyBoardView.m
//  BellManage
//
//  Created by Bestsch on 16/4/25.
//  Copyright © 2016年 wsl. All rights reserved.
//

#import "YcKeyBoardView.h"
#define addcommentNotification @"addcommentNotification"
@interface YcKeyBoardView()<UITextViewDelegate>
@property (nonatomic,assign) CGFloat textViewWidth;
@property (nonatomic,assign) BOOL isChange;
@property (nonatomic,assign) BOOL reduce;
@property (nonatomic,assign) CGRect originalKey;
@property (nonatomic,assign) CGRect originalText;
@end

@implementation YcKeyBoardView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor=[UIColor colorWithWhite:0.95 alpha:1];
        self.layer.borderColor=[UIColor colorWithWhite:0.8f alpha:1.0f].CGColor;
        self.layer.borderWidth=0.85f;
        [self initTextView:frame];
    }
    return self;
}

-(void)initTextView:(CGRect)frame
{
    self.textView=[[UITextView alloc]init];
    self.textView.returnKeyType = UIReturnKeySend;
    self.textView.enablesReturnKeyAutomatically = YES;
    self.textView.delegate=self;
    CGFloat textX=kStartLocation*0.5;
    self.textViewWidth=frame.size.width-2*textX;
    self.textView.frame=CGRectMake(textX, kStartLocation*0.2,self.textViewWidth , frame.size.height-2*kStartLocation*0.2);
    self.textView.backgroundColor=[UIColor clearColor];
    self.textView.layer.borderColor=[UIColor colorWithWhite:0.8f alpha:1.0f].CGColor;
    self.textView.layer.borderWidth=0.65f;
    self.textView.layer.cornerRadius=6.0f;
    self.textView.font=[UIFont systemFontOfSize:20.0];
    [self addSubview:self.textView];
}

- (void)textViewDidEndEditing:(UITextView *)textView {
    [self.textView resignFirstResponder];
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if ([text isEqualToString:@"\n"]){
        if ([self.delegate respondsToSelector:@selector(didSendCommentAction:)]) {
            [self.delegate didSendCommentAction:textView.text];
        }
        if([self.delegate respondsToSelector:@selector(keyBoardViewHide: textView:)]){
            
            [self.delegate keyBoardViewHide:self textView:self.textView];
        }
        return NO;
    }
    return YES;
}

-(void)textViewDidChange:(UITextView *)textView
{
    NSString *content=textView.text;
    
    CGSize contentSize=[content sizeWithFont:[UIFont systemFontOfSize:20.0]];
    if(contentSize.width>self.textViewWidth){
        
        if(!self.isChange){
            
            CGRect keyFrame=self.frame;
            self.originalKey=keyFrame;
            keyFrame.size.height+=keyFrame.size.height;
            keyFrame.origin.y-=keyFrame.size.height*0.25;
            self.frame=keyFrame;
            
            CGRect textFrame=self.textView.frame;
            self.originalText=textFrame;
            textFrame.size.height+=textFrame.size.height*0.5+kStartLocation*0.2;
            self.textView.frame=textFrame;
            self.isChange=YES;
            self.reduce=YES;
        }
    }
    if(contentSize.width<=self.textViewWidth){
        
        if(self.reduce){
            
            self.frame=self.originalKey;
            self.textView.frame=self.originalText;
            self.isChange=NO;
            self.reduce=NO;
        }
    }
}

@end
